package com.monitor.zabbix.service;


import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author chenhao 2015年9月9日 下午6:58:51
 */
public class HttpClientUtil {

    protected static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);

    /**
     * httpclient post 请求封装
     * 
     * @param url
     * @param params
     * @return
     */
    public static String postReq(String url, String content) throws Exception {

        URI uri = new URIBuilder(url).setCharset(Charset.forName("UTF-8")).build();

        HttpPost httppost = new HttpPost(uri);
        RequestConfig requestConfig =
                RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();// 设置请求和传输超时时间
        httppost.setConfig(requestConfig);

        StringEntity stringEntity = new StringEntity(content, ContentType.APPLICATION_JSON);
        httppost.setEntity(stringEntity);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            CloseableHttpResponse response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity);
                }
            } else {
                throw new Exception(String.format("the response status code is %d", statusCode));
            }
        } catch (IOException e) {
            throw new Exception(String.format("发送请求失败:%s", e.getMessage()), e);
        }
        return null;
    }



    /**
     * httpclient post 请求封装
     * 
     * @param url
     * @param params
     * @return
     */
    public static String getReq(String url) throws Exception {
        URI uri = new URIBuilder(url).setCharset(Charset.forName("UTF-8")).build();
        CloseableHttpClient httpclient = null;

        HttpGet httpGet = new HttpGet(uri);
        RequestConfig requestConfig =
                RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();// 设置请求和传输超时时间
        httpGet.setConfig(requestConfig);
        try {
            httpclient = HttpClients.createDefault();
            CloseableHttpResponse response = httpclient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    return EntityUtils.toString(entity);
                }
            } else {
                throw new Exception(String.format("the response status code is %d", statusCode));
            }
        } catch (IOException e) {
            throw new Exception(String.format("发送请求失败:%s", e.getMessage()), e);
        } finally {
            try {
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (IOException e) {
                logger.error("关闭httpclient错误", e);
            }
        }
        return null;
    }



    /**
     * 获取机器ip地址
     * */
    public static String getIPAddress() throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        return Arrays.toString(addr.getAddress());
    }



}
